package isDim;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.concurrent.BlockingQueue;

public class dimReciver implements Runnable{

	private int dimPort;
	private BlockingQueue<dimCargo> outbox;
	
	private final int BUFFER_SIZE = 1024*1024;
	
	public dimReciver(BlockingQueue<dimCargo> box, int port)
	{
		dimPort = port;
		outbox = box;
	}
	
	public void run()
	{
		DatagramSocket sock=null;
		byte[] buffer = new byte[BUFFER_SIZE];
		DatagramPacket packet = new DatagramPacket(buffer, BUFFER_SIZE); 

		// Open listening port.
		try {
			sock = new DatagramSocket(dimPort);
		}catch(Exception e){ System.out.print("FATAL ERROR. Cannot open dimPort: " + e.getMessage() + "\n"); System.exit(666); }
		
		while(true)
		{
			// Receive AirCoordination message and put it into outbox.
			try {
				sock.receive(packet);
				dimCargo cargo = new dimCargo(dimCargo.Type.AC_MSG, new String(buffer, 0, packet.getLength()));
				outbox.add(cargo);
				System.out.println("Package recived.");
			}catch(Exception e){ System.out.print("Error reciving packet from AirportCoordination: " + e.getMessage() + "\n"); }
			packet.setLength(BUFFER_SIZE);
		}
	}
}
